//
//  DepoistInfoTopCell.swift
//  d11
//
//  Created by peale on 2018/10/21.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class DepoistInfoTopCell: UITableViewCell {
    var bgImgView : UIImageView?
    var titleLabel : UILabel?
    var iconImgView : UIImageView?
    var lineView : UIView?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.white
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.bgImgView = UIImageView.init()
        self.bgImgView?.image = imageNamed("icon_my_cell_top")?.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),
                                                                                resizingMode: .stretch) //左右15像素的部分不变，中间部分来拉伸
        self.contentView.addSubview(bgImgView!);
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.centerX.equalTo(self.contentView)
            make.top.equalToSuperview()
        };
        self.titleLabel = UILabel.init()
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.textAlignment = .center
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.bgImgView?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-24)
            make.height.equalTo(30)
            make.centerX.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
        }
        
        self.iconImgView = UIImageView.init()
        self.bgImgView?.addSubview(self.iconImgView!)
        self.iconImgView?.snp.makeConstraints{(make) in
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.right.equalTo((self.titleLabel?.snp.left)!)
            make.centerY.equalTo((self.titleLabel?.snp.centerY)!)
        }
        
        self.lineView = UIView.init()
        self.lineView?.backgroundColor = HexRGBAlpha(0xF3F3F3, 1)
        self.bgImgView?.addSubview(self.lineView!)
        
        self.lineView?.snp.makeConstraints{(make) in
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-20)
            make.height.equalTo(0.7)
            make.bottom.equalToSuperview()
        }
    }
    
    func setData(dict:[String:Any]?) -> Void {
        let str =  dict!["title"] as? String ?? ""
        self.titleLabel?.text = str
        self.iconImgView?.sd_setImage(with: URL.init(string: dict!["imgName"] as? String ?? ""), completed: nil)
        let trect =  titleLabel?.getLableSize(text: str, maxWidth: kScreenWidth-24)
        self.titleLabel?.snp.remakeConstraints{ (make) in
            make.width.equalTo((trect?.width)!+10)
            make.height.equalTo(30)
            make.centerX.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
        }

        self.titleLabel?.layoutIfNeeded()
    }
}
